package com.tangqihang229350526.mynote

import android.graphics.drawable.Drawable
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.Target
import com.tangqihang229350526.mynote.database.NoteDatabase
import com.tangqihang229350526.mynote.databinding.ActivityNoteDetailBinding
import com.tangqihang229350526.mynote.repository.NoteRepository
import com.tangqihang229350526.mynote.viewmodel.DetailViewModel

class NoteDetailActivity : AppCompatActivity() {
    private var binding: ActivityNoteDetailBinding? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityNoteDetailBinding.inflate(layoutInflater)
        setContentView(binding!!.root)
        val id = intent.getIntExtra("id", -999)
        val viewModel = ViewModelProvider(this)[DetailViewModel::class.java]
        val database = NoteDatabase(this)
        viewModel.getNote().observe(this) { note ->
            binding!!.tvTitle.text = note.title
            binding!!.tvTime.text = note.time
            binding!!.tvAddress.text = note.address
            binding!!.tvContent.text = note.content
            binding!!.tvType.text = note.type
            Glide.with(this).load(note.image).into(binding!!.iv)
        }
        NoteRepository.instance?.queryById(database, id)?.let { viewModel.setNote(it) }
        binding!!.ivBack.setOnClickListener { finish() }
        binding!!.isDelete.setOnClickListener{ it ->
            NoteRepository.instance?.deleteById(database,id). apply { finish() }
        }
    }
}